package orders

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/orders"
	ordersReq "github.com/flipped-aurora/gin-vue-admin/server/model/orders/request"
	"gorm.io/gorm"
	"math/rand"
	"strconv"
)

type LsOrderService struct{}

// CreateLsOrder 创建lsOrder表记录
// Author [yourname](https://github.com/yourname)
func (lsOrderService *LsOrderService) CreateLsOrder(lsOrder *orders.LsOrder) (err error) {
	num := rand.Intn(90000000000) + 10000000000
	itoa := strconv.Itoa(num)
	lsOrder.OrderNumber = itoa
	err = global.GVA_DB.Create(lsOrder).Error
	return err
}

// DeleteLsOrder 删除lsOrder表记录
// Author [yourname](https://github.com/yourname)
func (lsOrderService *LsOrderService) DeleteLsOrder(ID string, userID uint) (err error) {
	err = global.GVA_DB.Transaction(func(tx *gorm.DB) error {
		if err := tx.Model(&orders.LsOrder{}).Where("id = ?", ID).Update("deleted_by", userID).Error; err != nil {
			return err
		}
		if err = tx.Delete(&orders.LsOrder{}, "id = ?", ID).Error; err != nil {
			return err
		}
		return nil
	})
	return err
}

// DeleteLsOrderByIds 批量删除lsOrder表记录
// Author [yourname](https://github.com/yourname)
func (lsOrderService *LsOrderService) DeleteLsOrderByIds(IDs []string, deleted_by uint) (err error) {
	err = global.GVA_DB.Transaction(func(tx *gorm.DB) error {
		if err := tx.Model(&orders.LsOrder{}).Where("id in ?", IDs).Update("deleted_by", deleted_by).Error; err != nil {
			return err
		}
		if err := tx.Where("id in ?", IDs).Delete(&orders.LsOrder{}).Error; err != nil {
			return err
		}
		return nil
	})
	return err
}

// UpdateLsOrder 更新lsOrder表记录
// Author [yourname](https://github.com/yourname)
func (lsOrderService *LsOrderService) UpdateLsOrder(lsOrder orders.LsOrder) (err error) {
	err = global.GVA_DB.Model(&orders.LsOrder{}).Where("id = ?", lsOrder.ID).Updates(&lsOrder).Error
	if lsOrder.OrderSataus == "待接单" {
		global.GVA_DB.Model(&orders.LsOrder{}).Where("order_status=?", "待接单").Update("制作中", lsOrder)
	}
	return err
}

// GetLsOrder 根据ID获取lsOrder表记录
// Author [yourname](https://github.com/yourname)
func (lsOrderService *LsOrderService) GetLsOrder(ID string) (lsOrder orders.LsOrder, err error) {
	err = global.GVA_DB.Where("id = ?", ID).First(&lsOrder).Error
	return
}

// GetLsOrderInfoList 分页获取lsOrder表记录
// Author [yourname](https://github.com/yourname)
func (lsOrderService *LsOrderService) GetLsOrderInfoList(info ordersReq.LsOrderSearch) (list []orders.LsOrder, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&orders.LsOrder{})
	var lsOrders []orders.LsOrder
	// 如果有条件搜索 下方会自动创建搜索语句
	if info.StartCreatedAt != nil && info.EndCreatedAt != nil {
		db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
	}
	if info.OrderNumber != nil && *info.OrderNumber != "" {
		db = db.Where("order_number = ?", *info.OrderNumber)
	}
	if info.GoodName != nil && *info.GoodName != "" {
		db = db.Where("good_name LIKE ?", "%"+*info.GoodName+"%")
	}
	if info.GetPeople != nil && *info.GetPeople != "" {
		db = db.Where("get_people = ?", *info.GetPeople)
	}
	if info.GetTel != nil && *info.GetTel != "" {
		db = db.Where("get_tel = ?", *info.GetTel)
	}
	if info.OrderSataus != nil && *info.OrderSataus != "" {
		db = db.Where("order_sataus = ?", *info.OrderSataus)
	}
	if info.DeliveryStatus != nil && *info.DeliveryStatus != "" {
		db = db.Where("delivery_status = ?", *info.DeliveryStatus)
	}
	if info.PayType != nil && *info.PayType != "" {
		db = db.Where("pay_type = ?", *info.PayType)
	}
	if info.PayStatus != nil && *info.PayStatus != "" {
		db = db.Where("pay_status = ?", *info.PayStatus)
	}
	err = db.Count(&total).Error
	if err != nil {
		return
	}

	if limit != 0 {
		db = db.Limit(limit).Offset(offset)
	}

	err = db.Find(&lsOrders).Error
	return lsOrders, total, err
}
func (lsOrderService *LsOrderService) GetLsOrderPublic() {
	// 此方法为获取数据源定义的数据
	// 请自行实现
}
